package com.basic.emg.service.impl;

import com.basic.common.exception.business.BusinessException;
import com.basic.common.exception.business.ValidationException;
import com.basic.emg.domain.EmergencyTeam;
import com.basic.emg.domain.PlanTeam;
import com.basic.emg.domain.TeamMember;
import com.basic.emg.domain.TeamMemberConnect;
import com.basic.emg.mapper.EmergencyMemberMapper;
import com.basic.emg.mapper.EmergencyTeamMapper;
import com.basic.emg.service.EmergencyTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EmergencyTeamServiceImpl implements EmergencyTeamService
{

    @Autowired
    private EmergencyTeamMapper teamMapper;
    @Autowired
    private EmergencyMemberMapper memberMapper;

    @Transactional
    @Override
    public void createTeam(EmergencyTeam team) {
        int i = teamMapper.insertTeam(team);
        if (i != 1){
            throw new BusinessException("创建失败！");
        }
        Long teamId = team.getId();

        List<TeamMember> teamMembers = team.getTeamMembers();
        if (!CollectionUtils.isEmpty(teamMembers)){
            for(TeamMember member : teamMembers){
                member.setTeamId(teamId);
                teamMapper.insertMember(member);
            }
        }

        /*List teamMeberList = new ArrayList();
        if (!CollectionUtils.isEmpty(teamMembers)){
            for(TeamMember member : teamMembers){
                teamMapper.insertMember(member);

                TeamMemberConnect teamMemberConnect = new TeamMemberConnect(team.getId(), member.getId());
                teamMeberList.add(teamMemberConnect);
            }
            teamMapper.insertTeamMembers(teamMeberList);
        }*/

    }

    @Override
    public List<EmergencyTeam> selectTeamList(EmergencyTeam team) {
        Example example = new Example(EmergencyTeam.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(team.getTeamName())){
            criteria.andEqualTo("teamName",team.getTeamName());
        }
        if (StringUtils.isNotBlank(team.getTeamRef())){
            criteria.andEqualTo("teamRef", team.getTeamRef());
        }
        if (StringUtils.isNotBlank(team.getBelongDept())){
            criteria.andEqualTo("belongDept",team.getBelongDept());
        }
        if(team.getStartTime1()!=null){
            criteria.andGreaterThanOrEqualTo("updateTime",team.getStartTime1());
        }
        if(team.getEndTime1()!=null){
            criteria.andLessThanOrEqualTo("updateTime",team.getEndTime1());
        }
        criteria.andNotEqualTo("delFlag","1");
        List<EmergencyTeam> emergencyTeams = teamMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(emergencyTeams)){
            Example example1 = new Example(TeamMember.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andNotEqualTo("delFlag","1");
            for (EmergencyTeam t : emergencyTeams){
                criteria1.andEqualTo("teamId", team.getId());
                List<TeamMember> teamMembers = memberMapper.selectByExample(example);
                t.setTeamMembers(teamMembers);
            }
        }
        return emergencyTeams;
    }

    @Override
    public EmergencyTeam selectTeamById(Long id) {
        EmergencyTeam team = teamMapper.selectByPrimaryKey(id);
        Example example = new Example(TeamMember.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teamId", team.getId());
        criteria.andNotEqualTo("delFlag","1");
        List<TeamMember> teamMembers = memberMapper.selectByExample(example);
        team.setTeamMembers(teamMembers);
        return team;
    }


    @Transactional
    @Override
    public void editTeam(EmergencyTeam team) {
        team.setUpdateTime(new Date());
        int i = teamMapper.updateByPrimaryKeySelective(team);
        if (i != 1){
            throw new BusinessException("更新失败！");
        }

        //删除原有关联人员
        Example example = new Example(TeamMember.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("teamId", team.getId());
        memberMapper.deleteByExample(example);

        //重新添加成员
        List<TeamMember> teamMembers = team.getTeamMembers();
        if (!CollectionUtils.isEmpty(teamMembers)){
            for(TeamMember member : teamMembers){
                member.setTeamId(team.getId());
                teamMapper.insertMember(member);
            }
        }
    }

    @Transactional
    @Override
    public void deleteTeam(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new ValidationException("id不能为空");
        }
        for(Long id : ids){
            //int i = teamMapper.deleteByPrimaryKey(id);
            EmergencyTeam team = new EmergencyTeam();
            team.setId(id);
            team.setDelFlag("1");
            teamMapper.updateByPrimaryKeySelective(team);
            Example example = new Example(TeamMember.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("teamId", id);
            TeamMember teamMember = new TeamMember();
            teamMember.setDelFlag("1");
            memberMapper.updateByExampleSelective(teamMember,example);
        }
    }
}
